<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\captcha\Captcha;
use think\facade\Session;
use think\facade\Config;
use think\Db;

class Events extends Base
{
    public function index()
    {
        $this->base_data['iszhong']=0;
        $data['base_data']=$this->base_data;
        $class_id = request()->param()['class_id'];
        $data['crumbs']=get_crumbs($class_id);/*面包屑*/
        $get=request()->param();
        if(!isset($get['page'])){
            $get['page']=1;
        }
        if($get['page']<1){
            header("Status: 404");
            return $this->fetch('404/404');
        }
        $count=Db::connect('publicdata')->table('lily_events')->where('status',1)->count('id');
        $count1=ceil($count/10);

        if($get['page']>$count1){
            header("Status: 404");
            return $this->fetch('404/404');
        }
    	$events=Db::connect('publicdata')->table('lily_events')->where('status',1)->order('create_time','desc')->paginate(10,false,['path'=>'',]);
    	//年份统计
        //$years=Db::query("SELECT COUNT(y) AS num,y FROM ( SELECT SUBSTRING(create_time, 1, 4) AS y,id FROM lily_events WHERE STATUS=1) AS a GROUP BY y ORDER BY y desc");
        $data['events']=$events;
        //$data['years']=$years;
        //分类统计
        $eventsc=Db::connect('publicdata')->query("select num,title,title_en,class_id from (SELECT COUNT(*) AS num,a.title,a.title_en,a.class_id,a.seq FROM lily_eventsclass as a INNER JOIN lily_events as b  on a.class_id=b.class_id where b.status=1  GROUP BY a.class_id ORDER BY a.class_id desc)as c where num>0 order by seq asc");
        $data['eventsc']=$eventsc;
        /*设置seo*/ 
        $default_lang=config('app.default_lang');
        $data['seo_desc']="";
        if($default_lang=='en-us'){
            $data['seo_title']="Lily Bearing News & events | Lily Bearing";
            $data['seo_keywords']="Lily Bearing News & events | Lily Bearing";
        }
        if($default_lang=='zh-cn'){
            $data['seo_title']="养贞轴承新闻事件 | 养贞轴承";
            $data['seo_keywords']="养贞轴承新闻事件 | 养贞轴承";
        }
    	$this->assign($data);
    	return $this->fetch();
    }
    public function detail($url)
    {
        $this->base_data['iszhong']=0;
    	$url=$url;
        if(strlen($url)==4&&is_numeric($url)){
           $this->archives($url,1);
           return $this->fetch('archives');
           exit; 
        }
    	$detail=Db::connect('publicdata')->table('lily_events')->where([['status','=',1],['url','=',$url]])->find();
        if(empty($detail)){
            header("Status: 404");
            return $this->fetch('404/404');
        }
        $detail['base_data']=$this->base_data;
        $class_id = 32;
        $detail['crumbs']=get_crumbs($class_id);/*面包屑*/
    	/*设置seo*/ 
        $detail['seo_title']=$detail['seo_title'];
        $detail['seo_keywords']=$detail['seo_key'];
        $detail['seo_desc']=$detail['seo_desc'];
        //最新文章
        $events=Db::connect('publicdata')->table('lily_events')->where('status',1)->order('create_time','desc')->paginate(10,false,['path'=>'',]);
        $detail['events']=$events;
        //分类统计
        $eventsc=Db::connect('publicdata')->query("select num,title,title_en,class_id from (SELECT COUNT(*) AS num,a.title,a.title_en,a.class_id,a.seq FROM lily_eventsclass as a INNER JOIN lily_events as b  on a.class_id=b.class_id where b.status=1  GROUP BY a.class_id ORDER BY a.class_id desc)as c where num>0 order by seq asc");
        $detail['eventsc']=$eventsc;
        //上一篇
        $detail['front']=Db::connect('publicdata')->table('lily_events')->where("id",'lt',$detail['id'])->order('id desc')->limit('1')->find();
        //下一篇
        $detail['after']=Db::connect('publicdata')->table('lily_events')->where("id",'gt',$detail['id'])->order('id desc')->limit('1')->find();
        //提取封面
        $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
        preg_match_all($pattern, $detail['content'], $matchContent);
        $detail['cover'] = isset($matchContent[1][0])?$matchContent[1][0]:"https://image.lily-bearing.com/images/class/ballbearing.png";
        //描点内容
        if($detail['miaodian']) {
            $content_ex = explode("</p>", htmlspecialchars_decode($detail['content']));
            $detail['miaocontent'] = str_replace("</a>", "</a></li>", str_replace("<a", "<li style='padding:5px 15px;text-align:left;'><a", $content_ex[0]));
            $detail['content'] = str_replace($content_ex[0], "", $detail['content']);
        }
        //第一段
        preg_match_all("/<p[^>]*>\s*.*\s*<\/p>/isU",$detail['content'],$match);
        $detail['first'] = isset($match[0][0])?strip_tags(strstr($match[0][0],"</p>",true)):$detail['title'];
        //作者
        $detail['author']=Db::connect('publicdata')->table('lily_admin')->where(['is_editor'=>1,'nickname'=>$detail['author']])->find();

        if($detail['miaodian']==1){
            $detail['last']=Db::connect('publicdata')->query("(select a.*,b.title as class_title,b.title_en from lily_events as a INNER JOIN lily_eventsclass as b on a.class_id=b.class_id  where a.id>".$detail['id']." and a.status=1 order by a.id asc limit 1 ) union (select a.*,b.title as class_title,b.title_en  from lily_events as a INNER JOIN lily_eventsclass as b on a.class_id=b.class_id where a.id<".$detail['id']." and a.status=1 order by a.id desc limit 1) union (select a.*,b.title as class_title,b.title_en from lily_events as a LEFT JOIN lily_eventsclass as b on a.class_id=b.class_id where a.id!=".$detail['id']." and a.status=1 ORDER BY a.id desc limit 4) ");
            $this->assign($detail);
            return $this->fetch('events/detail_miaodian');
        }else{
            $this->assign($detail);
            return $this->fetch();
        }
    }
    public function archives($y,$page=1)
    {
        $this->base_data['iszhong']=0;
        $data['base_data']=$this->base_data;
        $class_id = 32;
        $data['crumbs']=get_crumbs($class_id);/*面包屑*/
    	$year=['y'=>$y,'page'=>$page];
    	$events=Db::connect('publicdata')->table('lily_events')->where([['status','=',1],['create_time','like',$year['y']."%"]])->order('create_time','desc')->paginate(10,false,['query'=>$year,'path'=>'']);
    	$data['events']=$events;
        //年份统计
        $years=Db::connect('publicdata')->query("SELECT COUNT(y) AS num,y FROM ( SELECT SUBSTRING(create_time, 1, 4) AS y,id FROM lily_events WHERE STATUS=1) AS a GROUP BY y ORDER BY y desc");
        $data['years']=$years;
        //分类统计
        $eventsc=Db::connect('publicdata')->query("select num,title,title_en,class_id from (SELECT COUNT(*) AS num,a.title,a.title_en,a.class_id,a.seq FROM lily_eventsclass as a INNER JOIN lily_events as b  on a.class_id=b.class_id where b.status=1  GROUP BY a.class_id ORDER BY a.class_id desc)as c where num>0 order by seq asc");
        $data['eventsc']=$eventsc;
    	/*设置seo*/ 
        $data['seo_title']="Lily Bearing ".$year['y']." Year News & events | Lily Bearing";
        $data['seo_keywords']="Lily Bearing ".$year['y']." Year News & events | Lily Bearing";
        $data['seo_desc']="";
        $default_lang=config('app.default_lang');
        if($default_lang=='en-us'){
             $data['seo_title']="Lily Bearing ".$year['y']." News & events | Lily Bearing";
             $data['seo_keywords']="Lily Bearing ".$year['y']." News & events | Lily Bearing";
        }
        if($default_lang=='zh-cn'){
            $data['seo_title']="养贞轴承".$year['y']."年新闻事件 | 养贞轴承";
            $data['seo_keywords']="养贞轴承".$year['y']."年新闻事件 | 养贞轴承";
        }
        $data['y']=$y;
    	$this->assign($data);
    	//return $this->fetch();
    }

    public function eventsclass($title,$page=1)
    {
        $this->base_data['iszhong']=0;
        $data['base_data']=$this->base_data;
        $class_id = 32;
        $data['crumbs']=get_crumbs($class_id);/*面包屑*/
        $eventsclass=Db::connect('publicdata')->table('lily_eventsclass')->where([['title_en','=',str_replace('-',' ',$title)]])->find();
        if(empty($eventsclass)){
            header("Status: 404");
            return $this->fetch('404/404');
        }
        $class=['class_id'=>$eventsclass['class_id'],'page'=>$page];
        $events=Db::connect('publicdata')->table('lily_events')->where([['status','=',1],['class_id','=',$eventsclass['class_id']]])->order('create_time','desc')->paginate(10,false,['query'=>$class,'path'=>'']);
        $data['events']=$events;
        //年份统计
        //$years=Db::query("SELECT COUNT(y) AS num,y FROM ( SELECT SUBSTRING(create_time, 1, 4) AS y,id FROM lily_events WHERE STATUS=1) AS a GROUP BY y ORDER BY y desc");
        //$data['years']=$years;
        //分类统计
        $eventsc=Db::connect('publicdata')->query("select num,title,title_en,class_id from (SELECT COUNT(*) AS num,a.title,a.title_en,a.class_id,a.seq FROM lily_eventsclass as a INNER JOIN lily_events as b  on a.class_id=b.class_id where b.status=1  GROUP BY a.class_id ORDER BY a.class_id desc)as c where num>0 order by seq asc");
        $data['eventsc']=$eventsc;
        /*设置seo*/
        $data['seo_title']="Lily Bearing ".$eventsclass['title']." News & events | Lily Bearing";
        $data['seo_keywords']="Lily Bearing ".$eventsclass['title']." News & events | Lily Bearing";
        $data['seo_desc']="";
        $default_lang=config('app.default_lang');
        if($default_lang=='en-us'){
            $data['seo_title']="Lily Bearing ".ucwords(substr($eventsclass['title'],0,-1))." News & events | Lily Bearing";
            $data['seo_keywords']="Lily Bearing ".ucwords(substr($eventsclass['title'],0,-1))." News & events | Lily Bearing";
        }
        if($default_lang=='zh-cn'){
            $data['seo_title']="养贞轴承".lang($eventsclass['title'])."新闻事件 | 养贞轴承";
            $data['seo_keywords']="养贞轴承".lang($eventsclass['title'])."新闻事件 | 养贞轴承";
        }
        $data['eventsclass']=$eventsclass;
        $this->assign($data);
        return $this->fetch('eventsclass');
    }

    public function author($author,$page=1)
    {
        $this->base_data['iszhong']=0;
        $data['base_data']=$this->base_data;
        $class_id = 32;
        $data['crumbs']=get_crumbs($class_id);/*面包屑*/
        $authordata=Db::connect('publicdata')->table('lily_admin')->where([['nickname','=',ucwords(str_replace('-',' ',$author))]])->find();
        $class=['page'=>$page];
        $events=Db::connect('publicdata')->table('lily_events')->where([['status','=',1],['author','=',$authordata['nickname']]])->order('create_time','desc')->paginate(10,false,['query'=>$class,'path'=>'']);
        $data['events']=$events;
        //年份统计
        //$years=Db::query("SELECT COUNT(y) AS num,y FROM ( SELECT SUBSTRING(create_time, 1, 4) AS y,id FROM lily_events WHERE STATUS=1) AS a GROUP BY y ORDER BY y desc");
        //$data['years']=$years;
        //分类统计
        $eventsc=Db::connect('publicdata')->query("select num,title,title_en,class_id from (SELECT COUNT(*) AS num,a.title,a.title_en,a.class_id,a.seq FROM lily_eventsclass as a INNER JOIN lily_events as b  on a.class_id=b.class_id where b.status=1  GROUP BY a.class_id ORDER BY a.class_id desc)as c where num>0 order by seq asc");
        $data['eventsc']=$eventsc;
        /*设置seo*/
        $data['seo_title']="Lily Bearing ".$authordata['nickname']." News & events | Lily Bearing";
        $data['seo_keywords']="Lily Bearing ".$authordata['nickname']." News & events | Lily Bearing";
        $data['seo_desc']="";
        $default_lang=config('app.default_lang');
        if($default_lang=='en-us'){
            $data['seo_title']="Lily Bearing ".$authordata['nickname']." News & events | Lily Bearing";
            $data['seo_keywords']="Lily Bearing ".$authordata['nickname']." News & events | Lily Bearing";
        }
        if($default_lang=='zh-cn'){
            $data['seo_title']="养贞轴承".$authordata['nickname']."新闻事件 | 养贞轴承";
            $data['seo_keywords']="养贞轴承".$authordata['nickname']."新闻事件 | 养贞轴承";
        }
        $data['authordata']=$authordata;
        $this->assign($data);
        return $this->fetch();
    }
}    